package com.atstudying.mall.product.service.impl;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.Query;
import com.atstudying.mall.product.dao.AttrGroupDao;
import com.atstudying.mall.product.entity.AttrEntity;
import com.atstudying.mall.product.entity.AttrGroupEntity;
import com.atstudying.mall.product.service.AttrGroupService;
import com.atstudying.mall.product.service.AttrService;
import com.atstudying.mall.product.service.SpuInfoService;
import com.atstudying.mall.product.vo.AttrGroupWithAttrsVo;
import com.atstudying.mall.product.vo.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;

    @Autowired
    SpuInfoService spuInfoService;
    //默认的分页查询以前只会传入map参数
    /**map参数:如下
     * {
     page: 1,//当前页码
     limit: 10,//每页记录数
     sidx: 'id',//排序字段
     order: 'asc/desc',//排序方式
     key: '华为'//检索关键字
     }
     我们现在多写了一个catelogId，所以我们在写一个新方法queryPage
     */
//        PageUtils page = attrGroupService.queryPage(params);
    /**
     * {
     "msg": "success",
     "code": 0,
     "page": {
     "totalCount": 0,
     "pageSize": 10,
     "totalPage": 0,
     "currPage": 1,
     "list": [{
     "attrGroupId": 0, //分组id
     "attrGroupName": "string", //分组名
     "catelogId": 0, //所属分类
     "descript": "string", //描述
     "icon": "string", //图标
     "sort": 0 //排序
     "catelogPath": [2,45,225] //分类完整路径
     }]
     }
     }PageUtils返回给前端的数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //Map<String, Object> params中如果传入的参数由key（模糊匹配）
        //select * from pms_attr_group where catelog_id=? and (att_group_id=key or att_group_name like %key%)
        String key= (String) params.get("key");
        //让三级分类的id，等于我传过来的指定值
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();

        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });


        }
        //如果catelogId等于0就是查所有，不是等于0就是查指定的
        if (catelogId==0){
            //当前类有一个默认生成的方法page，传入两个参数（分页信息，查询信息）
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);

            return new PageUtils(page);

        }else {
            wrapper.eq("catelog_id",catelogId);

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     * 根据三级分类id，查找出所有的分组以及组的关联属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        //1：查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2：查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();

            //将分组（attrGroupEntities）里的数据，放到attrGroupWithAttrsVo里面
            BeanUtils.copyProperties(item,attrGroupWithAttrsVo);

            //以前在attrService中写过的getRelationAttr方法
            List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());

            attrGroupWithAttrsVo.setAttrs(attrs);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        /**
         * 思路：1：当前spu有多少属性分组和值   private String groupName; attrName attrValues
         */
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> spuItemAttrGroupVoList= baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
//        System.out.println(spuItemAttrGroupVoList+"...................");

        return spuItemAttrGroupVoList;
    }

}